package com.example.administrator.yanfoxconn.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TableRow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.bean.ExcePhoto;
import com.example.administrator.yanfoxconn.bean.JGBaseMessage;
import com.example.administrator.yanfoxconn.bean.SelectModel;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.constant.FoxContext;
import com.example.administrator.yanfoxconn.constant.ImageCaptureManager;
import com.example.administrator.yanfoxconn.intent.PhotoPickerIntent;
import com.example.administrator.yanfoxconn.intent.PhotoPreviewIntent;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.DateTimePickDialogUtil;
import com.example.administrator.yanfoxconn.utils.FileUtil;
import com.example.administrator.yanfoxconn.utils.HttpConnectionUtil;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.ImageZipUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONArray;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description 消毒劑配比 主界面
 * @Author song
 * @Date 6/17/21 8:41 AM
 */
public class JGCheckActivity extends BaseActivity implements View.OnClickListener{
    private final int MESSAGE_SET_PHOTO = 1;//掃描成功賦值
    private final int MESSAGE_TOAST = 2;//掃描失敗彈出框
    private final int MESSAGE_UP = 3;//提交信息
    private final int MESSAGE_JUMP = 4;//跳轉維護界面
    private final int MESSAGE_NETMISTAKE = 5;//網絡錯誤
    private final int MESSAGE_SET_CHECK = 6;

    private List<ExcePhoto> photos;
    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.btn_title_right)
    Button btnDel;//刪除

    @BindView(R.id.tv_address)
    TextView tvAddress;//消毒地點
    @BindView(R.id.tv_device)
    TextView tvDevice;//消毒設備
    @BindView(R.id.tv_method)
    TextView tvMethod;//消毒方式
    @BindView(R.id.tv_name)
    TextView tvName;//消毒劑名稱
    @BindView(R.id.tv_open_date)
    TextView tvOpenDate;//開瓶期限
    @BindView(R.id.tv_invalid_date)
    TextView tvInvalidDate;//失效期限
    @BindView(R.id.tv_pihao)
    TextView tvPihao;//消毒劑批號
    @BindView(R.id.tv_cheng_fen)
    TextView tvChenFen;//有效成分含量
    @BindView(R.id.tv_amount_xs)
    TextView tvXS;//消毒劑用量
    @BindView(R.id.tv_amount_water)
    TextView tvWater;//加水量
    @BindView(R.id.tv_nongdu)
    TextView tvNongDu;//終濃度
    @BindView(R.id.tv_unit)
    TextView tvUnit;//實施單位
    @BindView(R.id.tv_worker)
    TextView tvWork;//配置人員
    @BindView(R.id.tv_date)
    TextView tvDate;//配置時間
    @BindView(R.id.tr_amount_xs)
    TableRow tr_xs;//消毒劑用量行
    @BindView(R.id.tr_amount_water)
    TableRow tr_water;//加水量行
    @BindView(R.id.tr_nongdu)
    TableRow tr_nongdu;//終濃度行

    @BindView(R.id.ll_get_image)
    LinearLayout llGetImage;//異常圖片

    private JGBaseMessage messages;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jg_check_main);
        ButterKnife.bind(this);

        tvTitle.setText("配製詳情");
        btnBack.setText("返回");
        btnDel.setText("刪除");
        if (FoxContext.getInstance().getLoginId().equals("F2706331")||FoxContext.getInstance().getLoginId().equals("F3416918")){
        btnDel.setVisibility(View.VISIBLE);}
        btnDel.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        tvInvalidDate.setOnClickListener(this);

        messages=(JGBaseMessage) getIntent().getSerializableExtra("msg");
        tvAddress.setText(messages.getM_ADDRESS());
        tvDevice.setText(messages.getM_DEVICE());
        tvMethod.setText(messages.getM_METHOD());
        tvName.setText(messages.getM_NAME());
        tvOpenDate.setText(messages.getM_OPEN_DATE());
        tvInvalidDate.setText(messages.getM_INVALID_DATE());
        tvPihao.setText(messages.getM_PIHAO());
        tvChenFen.setText(messages.getM_CHENGFEN());
        tvXS.setText(messages.getM_AMOUNT_XS());
        tvWater.setText(messages.getM_AMOUNT_WATER());
        tvNongDu.setText(messages.getM_NONGDU());
        tvUnit.setText(messages.getM_UNIT());
        tvWork.setText(messages.getM_WORKER());
        tvDate.setText(messages.getM_DATE());
        if (messages.getM_NAME().equals("酒精")){
            tr_nongdu.setVisibility(View.GONE);
            tr_xs.setVisibility(View.GONE);
            tr_water.setVisibility(View.GONE);
        }
        getPhoto(messages.getM_ID());
    }




    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case MESSAGE_TOAST://Toast彈出

                    ToastUtils.showLong(JGCheckActivity.this, msg.obj.toString());
                    finish();
                    break;

                case MESSAGE_NETMISTAKE://Toast彈出;
                    ToastUtils.showLong(JGCheckActivity.this, "网络问题请稍后重试！");
//                    finish();
                    break;
                case MESSAGE_SET_PHOTO://text賦值
                    for (int i = 0; i < photos.size(); i++) {
                        ImageView imageView = new ImageView(JGCheckActivity.this);
                        String imgStr = photos.get(i).getIMAGE();
                        Log.e("------imgStr-----", "======" + imgStr);
                        byte[] decode = Base64.decode(imgStr, Base64.DEFAULT);
                        Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
                        imageView.setImageBitmap(bitmap);
                        llGetImage.addView(imageView);
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_title_left:
                 finish();
                 break;
            case R.id.btn_title_right:
                worningAlert("確認刪除？",MESSAGE_TOAST);

                break;
        }
    }

    private void worningAlert(String msg, final int type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息")
                .setMessage(msg)
                .setCancelable(false)
                .setPositiveButton("確認", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        // TODO Auto-generated method stub
                        if (type==MESSAGE_TOAST){
                            del(messages.getM_ID());
                        }
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }

private void del(String mId){
    showDialog();
    final String url = Constants.HTTP_XS_MAKE_DELETE_SERVLET + "&m_id=" + mId;
    Log.e("-------fff--", "url==" + url);
    new Thread() {
        @Override
        public void run() {
            result = HttpUtils.queryStringForGet(url);
            dismissDialog();
            Gson gson = new Gson();
            //Log.e("--fff---result----", result.toString());
            if (result != null) {

                JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                String errCode = jsonObject.get("errCode").getAsString();
                if (errCode.equals("200")) {

                    Message message = new Message();
                    message.what = MESSAGE_TOAST;
                    message.obj = jsonObject.get("errMessage").getAsString();
                    mHandler.sendMessage(message);
                } else if (errCode.equals("400")) {
                    Message message = new Message();
                    message.what = MESSAGE_TOAST;
                    message.obj = jsonObject.get("errMessage").getAsString();
                    mHandler.sendMessage(message);
                }
            } else {
                Message message = new Message();
                message.what = MESSAGE_TOAST;
                message.obj = "請求不成功";
                mHandler.sendMessage(message);
            }
        }
    }.start();
}
    private String result;

    private void getPhoto(String mId) {
        showDialog();
        final String url = Constants.HTTP_XS_MALE_RECORD_PHOTO + "m_id=" + mId;
        Log.e("-------fff--", "url==" + url);
        new Thread() {
            @Override
            public void run() {
                result = HttpUtils.queryStringForGet(url);
                dismissDialog();
                Gson gson = new Gson();
                //Log.e("--fff---result----", result.toString());
                if (result != null) {

                    JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                    String errCode = jsonObject.get("errCode").getAsString();
                    if (errCode.equals("200")) {

                        JsonArray array = jsonObject.get("data").getAsJsonArray();
                        photos = new ArrayList<ExcePhoto>();
                        for (JsonElement type : array) {
                            ExcePhoto humi = gson.fromJson(type, ExcePhoto.class);
                            photos.add(humi);
                        }
                        Message message = new Message();
                        message.what = MESSAGE_SET_PHOTO;
                        message.obj = photos;
                        mHandler.sendMessage(message);
                    } else if (errCode.equals("400")) {
                        Message message = new Message();
                        message.what = MESSAGE_TOAST;
                        message.obj = jsonObject.get("errMessage").getAsString();
                        mHandler.sendMessage(message);
                    }
                } else {
                    Message message = new Message();
                    message.what = MESSAGE_TOAST;
                    message.obj = "請求不成功";
                    mHandler.sendMessage(message);
                }
            }
        }.start();
    }


}
